<template>
  <div class="channel-edit">
    <!-- 顶部 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        class="edit-btn"
        type="danger"
        plain
        round
        size="mini"
        @click="isEdit = !isEdit"
        >{{ isEdit ? "编辑" : "完成" }}</van-button
      >
    </van-cell>
    <!-- grid网格 -->

    <van-grid :gutter="10">
      <van-grid-item
        class="grid-item my-grid"
        v-for="(item, index) in myChannels"
        :key="item.id"
        @click="onMyChannelClick(item, index)"
      >
        <van-icon
          slot="icon"
          name="clear"
          v-show="isEdit && !fixedChannels.includes(item.id)"
        ></van-icon>
        <span
          slot="text"
          class="text"
          :class="{ Curactive: active == index }"
          >{{ item.name }}</span
        >
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title">频道推荐</div>
    </van-cell>
    <!-- grid网格 -->

    <van-grid :gutter="10">
      <van-grid-item
        class="grid-item recomment-grid"
        v-for="value in recommentChannels"
        :key="value.id"
        icon="plus"
        :text="value.name"
        @click="onAddChannel(value)"
      />
    </van-grid>
    <!-- /频道推荐 -->
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel,
} from "@/api/channel.js";
import { mapState } from "vuex";
import { setItem } from "@/utils/storage";
export default {
  name: "ChannelEdit",
  data() {
    return {
      allChannels: [],
      isEdit: false,
      fixedChannels: [0],
    };
  },
  props: {
    myChannels: {
      type: Array,
      required: true,
    },
    active: {
      type: Number,
      required: true,
    },
  },
  computed: {
    recommentChannels() {
      return this.allChannels.filter((item) => {
        return !this.myChannels.find((it) => it.id == item.id);
      });

      //   const channels = [];
      //   this.allChannels.forEach((item) => {
      //     const res = this.myChannels.find((it) => {
      //       return it.id == item.id;
      //     });
      //     if (!res) {
      //       channels.push(item);
      //     }
      //   });
      //   return channels;
    },
    ...mapState(["user"]),
  },
  created() {
    this.loadAllChannels();
  },
  methods: {
    async loadAllChannels() {
      try {
        const {
          data: {
            data: { channels },
          },
        } = await getAllChannels();
        this.allChannels = channels;
      } catch (err) {
        console.log("请求错误!");
      }
    },
    // 点击添加选项
    async onAddChannel(channel) {
      //1.添加数据到数组
      this.myChannels.push(channel);
      if (this.user) {
        // 2.登录 放在线上;
        await addUserChannel({
          id: channel.id,
          seq: this.myChannels.length,
        });
        try {
          console.log(1);
        } catch (err) {
          console.log("添加失败!");
          this.$toast("保存失败,请稍后重试!");
        }
      } else {
        // 3.没登录 储存到本地
        setItem("TOUTIAO_CHANNELS", this.myChannels);
      }
    },
    onMyChannelClick(channel, index) {
      // console.log(channel,index)
      if (this.fixedChannels.includes(channel.id)) {
        return; //如果是固定频道则不删除;
      }
      // 1.编辑状态执行删除
      // 2.非编辑状态执行跳转
      if (this.isEdit) {
        //编辑==>删除
        this.myChannels.splice(index, 1);
        if (index <= this.active) {
          this.$emit("update-active", this.active - 1, true);
        }

        // 删除频道
        this.deleteChannel(channel);
      } else {
        //非编辑==>跳转
        this.$emit("update-active", index, false);
      }
    },
    async deleteChannel(channel) {
      try {
        if (this.user) {
          //已经登录 更新线上
          await deleteUserChannel(channel.id);
          console.log("删除了!");
        } else {
          //未登录 储存本地;
          setItem("TOUTIAO_CHANNELS", this.myChannels);
          console.log("未登录 删除!");
        }
      } catch (err) {
        console.log("删除失败,请稍后再试!");
      }
    },
  },
};
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;

  .title-text {
    font-size: 32px;
    color: #333;
  }
  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  /deep/.van-icon {
    color: #666 !important;
  }
  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin: 0;
      }
      .Curactive {
        color: #f00;
      }
    }
  }
  /deep/ .recomment-grid {
    .van-grid-item__content {
      flex-direction: row;
      .van-icon-plus {
        font-size: 28px;
        margin-right: 6px;
      }
      .van-grid-item__text {
        margin: 0;
      }
    }
  }

  /deep/.van-icon-clear {
    position: absolute;
    right: -10px;
    top: -10px;
    font-size: 30px;
    color: #cacaca !important;
    z-index: 2;
  }
  /deep/.van-grid-item__icon-wrapper {
    position: unset;
  }
}
</style>
